using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data;
using DDF.Data.Providers;
using System.Data;
using System.Linq.Expressions;
using System.Configuration;
using ARR.Entities.Gateways;

namespace ARR.DataModel.Repositories
{
	public static partial class MonthsRepository
	{
		private static string connString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
		
		public class MonthsDbo : DatabaseObject
        {
            public MonthsDbo()
            {
                Name = "Months";
                GeneratedClassName = "Months";
                SequenceName = "N/A";
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Date", ColumnType = "date", IsIdentity = false, IsNullable = false, Length = 3, Precision = 10, Scale = 0, GeneratedPropertyName = "Date" });
              
            }
        }
		
		public static List<MonthRow> Select(Expression<Func<MonthRow, bool>> expression)
        {
            List<MonthRow> rows = new List<MonthRow>();

            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                IDataReader reader = database.Select<MonthRow>(new MonthsDbo(), expression);

                Load(reader, rows);
            }

            return rows;
        }
		
		public static List<MonthRow> Select()
		{
			List<MonthRow> rows = new List<MonthRow>();

			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.Select<MonthRow>(new MonthsDbo(), null);

				Load(reader, rows);
			}

			return rows;
		}
		
		public static List<MonthRow> PagedSelect(Expression<Func<MonthRow, bool>> filterExpression, Expression<Func<MonthRow, object>> orderByExpression, int pageNumber, int pageSize)
		{
			List<MonthRow> rows = new List<MonthRow>();
			
			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.PagedSelect<MonthRow>(new MonthsDbo(), filterExpression, orderByExpression, pageNumber, pageSize);

				Load(reader, rows);
			}

			return rows;
		}
		
		private static void Load(IDataReader reader, List<MonthRow> MonthList)
        {
            MonthRow row;

            while (reader.Read())
            {
                row = new MonthRow()
                {
					Date = Util.CastDbValue<DateTime>(reader["Date"])
                };
                                
                MonthList.Add(row);
            }

            reader.Close();
        }

			
		public static void Insert(MonthRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Insert<MonthRow>(new MonthsDbo(), row);
            }
        }

		public static void Update(MonthRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Update<MonthRow>(new MonthsDbo(), row);
            }
        }

		public static void Delete(Expression<Func<MonthRow, bool>> expression)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Delete<MonthRow>(new MonthsDbo(), expression);
            }
        }

	}
}
	
